package com.example.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

public class MainActivity extends AppCompatActivity {
    private EditText edt_name;
    private EditText edt_pwd;
    private EditText edt_age;
    private EditText edt_sex;
    private Button btn_reg;
    private Button btn_log;
    private Button btn_info;

    private EditText edt_s_name;
    private EditText edt_s_age;
    private EditText edt_profession;
    private EditText edt_score;
    private Button btn_add;
    private Button btn_query;
    private Button btn_del;
    private ImageView iv_head;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initUser();

        initStudent();

        findViewById(R.id.btn_stu_list).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, StuListActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initStudent() {
        edt_s_name = findViewById(R.id.edt_s_name);
        edt_s_age = findViewById(R.id.edt_s_age);
        edt_profession = findViewById(R.id.edt_profession);
        edt_score = findViewById(R.id.edt_score);
        btn_add = findViewById(R.id.btn_add);
        btn_query = findViewById(R.id.btn_query);
        btn_del = findViewById(R.id.btn_del);
        iv_head = findViewById(R.id.iv_head);

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addStudent();
            }
        });

        btn_query.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                queryStudent();
            }
        });

        btn_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delStudent();
            }
        });
    }

    //删除学生
    private void delStudent() {
        BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
        studentBmobQuery.addWhereEqualTo("profession", "软件");
        studentBmobQuery.addWhereEqualTo("age", 18);
        studentBmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if(e == null){
                    for(Student student: list){
                        student.delete(new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                                if(e == null){
                                    toast("删除成功");
                                }else{
                                    toast("删除失败 " + e.getMessage());
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    //查询学生
    private void queryStudent() {
        BmobQuery<Student> studentBmobQuery = new BmobQuery<>();
        studentBmobQuery.addWhereEqualTo("profession", "软件");
        studentBmobQuery.addWhereEqualTo("age", 18);
        studentBmobQuery.findObjects(new FindListener<Student>() {
            @Override
            public void done(List<Student> list, BmobException e) {
                if(e == null){
                    StringBuffer buffer = new StringBuffer();
                    for(Student student: list){
                        buffer.append(student).append("\n");
                        BmobFile headimg = student.getHeadimg();
                        Log.e("Bmob", "FileUrl = " + headimg.getFileUrl());
                        Log.e("Bmob", "Url = " + headimg.getUrl());
                        Glide.with(MainActivity.this).load(headimg.getUrl()).into(iv_head);
                    }

                    toast(buffer.toString());
                }
            }
        });
    }

    //添加学生
    private void addStudent() {
        //获取本地资源
        String fileUrl = "/mnt/shared/Image/icon1.jpg";
        final BmobFile headimg = new BmobFile(new File(fileUrl));
        //上传到网络应用中
        headimg.upload(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    Log.e("Bmob", "文件上传成功");

                    Student student = new Student();
                    student.setName(edt_s_name.getText().toString());
                    student.setAge(Integer.parseInt(edt_s_age.getText().toString()));
                    student.setProfession(edt_profession.getText().toString());
                    student.setScore(Integer.parseInt(edt_score.getText().toString()));
                    student.setHeadimg(headimg);
                    student.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                toast("保存成功 " + s);
                            } else {
                                toast("保存失败 " + e.getMessage());
                            }
                        }
                    });
                }else {
                    Log.e("Bmob", "文件上传失败");
                }
            }
        });
    }

    private void initUser() {
        edt_name = findViewById(R.id.edt_name);
        edt_pwd = findViewById(R.id.edt_pwd);
        edt_age = findViewById(R.id.edt_age);
        edt_sex = findViewById(R.id.edt_sex);
        btn_reg = findViewById(R.id.btn_reg);
        btn_log = findViewById(R.id.btn_log);
        btn_info = findViewById(R.id.btn_info);

        btn_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //useBmobUserReg();
                useMyBmobUserReg();
            }
        });

        btn_log.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useBmobUserLogin();
            }
        });

        btn_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useBmobUserInfo();
            }
        });
    }

    //用户修改操作
    private void useBmobUserInfo() {
        //只能修改当前用户
        MyBmobUser myBmobUser = BmobUser.getCurrentUser(MyBmobUser.class);
        myBmobUser.setAge(Integer.parseInt(edt_age.getText().toString()));
        myBmobUser.setSex(edt_sex.getText().toString());
        myBmobUser.update(new UpdateListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    toast("修改成功");
                }else{
                    toast("修改失败 " + e.getMessage());
                }
            }
        });
    }

    //使用自定义的User进行注册
    private void useMyBmobUserReg() {
        MyBmobUser myBmobUser = new MyBmobUser();
        myBmobUser.setUsername(edt_name.getText().toString());
        myBmobUser.setPassword(edt_pwd.getText().toString());
        myBmobUser.setAge(Integer.parseInt(edt_age.getText().toString()));
        myBmobUser.setSex(edt_sex.getText().toString());
        myBmobUser.signUp(new SaveListener<MyBmobUser>() {
            @Override
            public void done(MyBmobUser myBmobUser, BmobException e) {
                if(e == null){
                    toast("注册成功 " + myBmobUser.getUsername());
                }else{
                    toast("注册失败 " + e.getMessage());
                }
            }
        });
    }

    //用户登录
    private void useBmobUserLogin() {
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(edt_name.getText().toString());
        bmobUser.setPassword(edt_pwd.getText().toString());
        bmobUser.login(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if(e == null){
                    toast("登录成功");
                }else{
                    toast("登录失败 " + e.getMessage());
                }
            }
        });
    }

    //使用Bmob进行用户注册
    private void useBmobUserReg() {
        BmobUser bmobUser = new BmobUser();
        bmobUser.setUsername(edt_name.getText().toString());
        bmobUser.setPassword(edt_pwd.getText().toString());
        bmobUser.signUp(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if(e == null){
                    toast("注册成功 " + bmobUser.getUsername());
                }else{
                    toast("注册失败 " + e.getMessage());
                }
            }
        });
    }

    private void toast(String s) {
        Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
    }
}
